#pragma once


namespace Test {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::IO::Ports::SerialPort^  port;
	protected: 
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::TextBox^  channel;
	private: System::Windows::Forms::TextBox^  angle;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::ComponentModel::IContainer^  components;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>


#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			this->port = (gcnew System::IO::Ports::SerialPort(this->components));
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->channel = (gcnew System::Windows::Forms::TextBox());
			this->angle = (gcnew System::Windows::Forms::TextBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// port
			// 
			this->port->BaudRate = 2400;
			this->port->PortName = L"COM5";
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(20, 16);
			this->button1->Margin = System::Windows::Forms::Padding(5, 4, 5, 4);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(125, 30);
			this->button1->TabIndex = 0;
			this->button1->Text = L"SET";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// channel
			// 
			this->channel->Location = System::Drawing::Point(155, 18);
			this->channel->Margin = System::Windows::Forms::Padding(5, 4, 5, 4);
			this->channel->Name = L"channel";
			this->channel->RightToLeft = System::Windows::Forms::RightToLeft::Yes;
			this->channel->Size = System::Drawing::Size(164, 26);
			this->channel->TabIndex = 1;
			this->channel->Text = L"1";
			// 
			// angle
			// 
			this->angle->Location = System::Drawing::Point(332, 18);
			this->angle->Margin = System::Windows::Forms::Padding(5, 4, 5, 4);
			this->angle->Name = L"angle";
			this->angle->RightToLeft = System::Windows::Forms::RightToLeft::Yes;
			this->angle->Size = System::Drawing::Size(164, 26);
			this->angle->TabIndex = 2;
			this->angle->Text = L"1500";
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(203, 48);
			this->label1->Margin = System::Windows::Forms::Padding(5, 0, 5, 0);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(58, 17);
			this->label1->TabIndex = 3;
			this->label1->Text = L"Zglob";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(378, 48);
			this->label2->Margin = System::Windows::Forms::Padding(5, 0, 5, 0);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(68, 17);
			this->label2->TabIndex = 4;
			this->label2->Text = L"Ugao \?";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(10, 17);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(546, 90);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->angle);
			this->Controls->Add(this->channel);
			this->Controls->Add(this->button1);
			this->Font = (gcnew System::Drawing::Font(L"CourierPS", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->Margin = System::Windows::Forms::Padding(5, 4, 5, 4);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->FormClosed += gcnew System::Windows::Forms::FormClosedEventHandler(this, &Form1::Form1_FormClosed);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
	{
		try {
			int val = Convert::ToInt32( angle->Text );
			int ch = Convert::ToInt32( channel->Text );
			array<unsigned char, 1> ^ data = gcnew array<unsigned char, 1>(8);
			data[0] = '!';
			data[1] = 'S';
			data[2] = 'C';
			data[3] = (unsigned char) ch;
			data[4] = 13; // ramp - brzina kaze misa
			data[5] = (unsigned char) (val & 0xFF);
			data[6] = (unsigned char) ((val>>8) & 0xFF);
			data[7] = 0x0D;
			port->Write(data, 0, 8);
		}
		catch(...)
		{
			angle->Text = L" Parameter ERROR ";
		}
	}

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) 
		 {
			 this->port->PortName = L"COM6";
			 port->Open();
		 }
private: System::Void Form1_FormClosed(System::Object^  sender, System::Windows::Forms::FormClosedEventArgs^  e) 
		 {
			 port->Close();
		 }
};
}

